import java.util.BitSet;

public class RedisUtils {
    // 将BitSet转换为字符串格式
    public static String bitSetToString(BitSet bits, int rows, int cols) {
        StringBuilder sb = new StringBuilder(rows * cols);
        for (int y = 0; y < rows; y++) {
            for (int x = 0; x < cols; x++) {
                int index = y * cols + x;
                sb.append(bits.get(index) ? '1' : '0');
            }
        }
        return sb.toString();
    }

    // 将字符串转换为BitSet
    public static BitSet stringToBitSet(String data, int rows, int cols) {
        int totalBits = rows * cols;
        BitSet bits = new BitSet(totalBits);
        for (int i = 0; i < Math.min(data.length(), totalBits); i++) {
            if (data.charAt(i) == '1') {
                bits.set(i);
            }
        }
        return bits;
    }

    // 格式化为可读的网格字符串
    public static String formatBitSet(BitSet bits, int rows, int cols) {
        StringBuilder sb = new StringBuilder("\n");
        for (int y = 0; y < rows; y++) {
            for (int x = 0; x < cols; x++) {
                int index = y * cols + x;
                sb.append(bits.get(index) ? "1" : "0");
            }
            if (y < rows - 1) sb.append("\n");
        }
        return sb.toString();
    }
}
